//
//  ZMainTabBarViewController.swift
//  swift-weibo
//
//  Created by zero on 17/3/19.
//  Copyright © 2017年 zero. All rights reserved.
//

import UIKit

class ZMainTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor.white
        
        getChildViewControllers()
        
        let zTabBar = ZTabBar()
        
        setValue(zTabBar, forKey: "tabBar")
        
        zTabBar.btnClick = {(tabbar) -> () in
            
            ZComposeView.show(targer: self)
            
        }
        
    }


    func getChildViewControllers()  {
        
//        addChildViewController(getViewController(className: "ZHomeTableViewController", title: "主页", image: "tabbar_home", imageH: "tabbar_home_selected"))
//        addChildViewController(getViewController(className: "ZMessageTableViewController", title: "消息", image: "tabbar_message_center", imageH: "tabbar_message_center_selected"))
//        addChildViewController(getViewController(className: "ZDiscoverTableViewController", title: "发现", image: "tabbar_discover", imageH: "tabbar_discover_selected"))
//        addChildViewController(getViewController(className: "ZProfileTableViewController", title: "我的", image: "tabbar_profile", imageH: "tabbar_profile_selected"))
        
        addChildViewController(getChileViewControll(vc: ZHomeTableViewController(), title: "首页", imageName: "tabbar_home"))
        
        addChildViewController(getChileViewControll(vc: ZMessageTableViewController(), title: "消息", imageName: "tabbar_message_center"))
        
        addChildViewController(getChileViewControll(vc: ZDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover"))
        
        addChildViewController(getChileViewControll(vc: ZProfileTableViewController(), title: "我的", imageName: "tabbar_profile"))
        
    }
    
    
    private func getChileViewControll(vc: UIViewController,title:String,imageName:String) -> UIViewController {
        
        vc.title = title;
        
        vc.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        
        
        
        return ZMainNavigationViewController(rootViewController: vc)
    }
    
    private func getViewController(className: String,title:String,image:String,imageH:String) -> UIViewController {
        
        //  获取命名空间
        guard let clsName = Bundle.main.infoDictionary?["CFBundleExecutable"] else {
            
            print("命名空间不存在")
            
            return UIViewController()
        }
        
        // 2.通过命名空间和类名转换成类
        let cls : AnyClass? = NSClassFromString((clsName as! String) + "." + className)
        
        // swift 中通过Class创建一个对象,必须告诉系统Class的类型
        guard let clsType = cls as? UIViewController.Type else {
            
            print("无法转换成UIViewController")
            return UIViewController()
        }
        
        //  通过Class 创建对象
        let childController = clsType.init()
        
        //  设置tabbar属性
        childController.title = title
        childController.tabBarItem.image = UIImage(named:image)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named:imageH)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //        childController.tabBarController?.tabBar.
        tabBar.isTranslucent = false
        tabBar.tintColor = UIColor.orange
        tabBar.barTintColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
        
        
        let viewController = ZMainNavigationViewController(rootViewController: childController)
        
        
        return viewController
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    



}
